﻿using Google.Protobuf.Collections;
using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class LikePanelData
{
    public List<long> likes;
    public List<long> beLikes;
}

public class LikePanel : AppUI
{
    List<LikeItem> m_BelikeItems = new List<LikeItem>();
    List<LikeItem> m_Likes = new List<LikeItem>();
    public Transform likeItemsParent;
    public Transform beLikeItemsParent;

    public LikeItem likeItemPref;
    public LikeItem belikeItemPref;

    public GameObject likeItemsObj;
    public GameObject beLiketemsObj;

    LikePanelData m_Data;

    public static int lastSelectToggle = 1;

    public const int Toggle_BeLikes = 1;
    public const int Toggle_Likes = 2;

    public Text totalBeLikeCount;
    public Text totalLikeCount;
    public ListTogglesGroup togglesGroup;

    public override void Start()
    {
        if (togglesGroup)
            togglesGroup.Init(OnToggleSelect, lastSelectToggle);

        likeItemPref.gameObject.SetActive(false);
        belikeItemPref.gameObject.SetActive(false);
    }

    public void Toggle(int id)
    {
        lastSelectToggle = id;

        if (togglesGroup)
            togglesGroup.Init(OnToggleSelect, lastSelectToggle);
    }

    void OnToggleSelect(int id)
    {
        lastSelectToggle = id;

        if (id == Toggle_BeLikes)
        {
            likeItemsObj.gameObject.SetActive(false);
            beLiketemsObj.gameObject.SetActive(true);

            StartCoroutine(UnityFunctions.AdjustItemsLayout(beLiketemsObj.transform));
         
        }
        else if (id == Toggle_Likes)
        {
            likeItemsObj.gameObject.SetActive(true);
            beLiketemsObj.gameObject.SetActive(false);

            StartCoroutine(UnityFunctions.AdjustItemsLayout(likeItemsObj.transform));
        }
    }

    public void Init(LikePanelData data)
    {
        m_Data = data;

        if (lastSelectToggle == Toggle_BeLikes)
        {
            RefreshBeLikes(()=>RefreshLikes(null));
        }
        else
        {
            RefreshLikes(() => RefreshBeLikes(null));
        }
    }

    void RefreshBeLikes(Action onEnd)
    {
        var belikes = m_Data.beLikes.AllWhere(e => e > 0).ToArray();
        this.totalBeLikeCount.text = belikes.Length + "人喜欢你";

        UnityFunctions.ClearList(m_BelikeItems);
        RequestServerController.instance.RequestMiniUserInfo(belikes, (userInfos) =>
        {
            if (userInfos != null)
            {
                for (int i = 0; i < userInfos.Length; i++)
                {
                    MiniUserInfo visitor = userInfos[i];
                    var newItem = Instantiate(belikeItemPref, beLikeItemsParent);
                    newItem.gameObject.SetActive(true);
                    newItem.Init(visitor, OnSelectItem);

                    m_BelikeItems.Add(newItem);
                }
                StartCoroutine(UnityFunctions.AdjustItemsLayout(transform));
            }
            onEnd?.Invoke();
        });

    }

    void RefreshLikes(Action onEnd)
    {
        var likes = m_Data.likes.AllWhere(e => e > 0).ToArray();
        this.totalLikeCount.text = $"喜欢了{likes.Length}人";

        UnityFunctions.ClearList(m_Likes);
        RequestServerController.instance.RequestMiniUserInfo(likes, (userInfos) =>
        {
            if (userInfos != null)
            {
                for (int i = 0; i < userInfos.Length; i++)
                {
                    MiniUserInfo visitor = userInfos[i];
                    var newItem = Instantiate(likeItemPref, likeItemsParent);
                    newItem.gameObject.SetActive(true);
                    newItem.Init(visitor, OnSelectItem);

                    m_Likes.Add(newItem);
                }
            }
            onEnd?.Invoke();
        });
    }
   
    public void OnSelectItem(LikeItem item)
    {
        var userHome = this.GetComponentInParent<UIController>().ShowUI<UserHomePanel>();

        RequestServerController.instance.RequestUserMomentInfo(item.data.userId, (userInfo) =>
        {
            userHome.Init(userInfo);
        });

        RequestServerController.instance.ReqVisit(MGame.instance.MUser.id, item.data.userId, (v) =>
        {
            GameDebug.Log($"ReqVisit {item.data.userId} end");
        });
    }
    // Update is called once per frame
    void Update()
    {

    }

    //public override void GoBack()
    //{
    //    UIController.ShowUI<MainPanel>();
    //}

}
